package com.example.administrator.managesystem.view;

import android.view.View;
import android.widget.LinearLayout;


import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.ui.BasePicker;
import com.example.administrator.managesystem.ui.IPickerDialog;
import com.example.administrator.managesystem.ui.TimePicker;
import com.example.administrator.managesystem.utils.DateUtil;
import com.example.administrator.managesystem.utils.TimeUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by wr
 * on 2019/9/30
 */
public class TimePickerDialog extends BaseBottomDialog implements IPickerDialog, TimePicker.OnTimeSelectListener {


    private TimePicker mTimePicker;
    public static final SimpleDateFormat mDateFormat =
            new SimpleDateFormat("MM月dd日  E", Locale.CHINA);
    private TimeClickListener listener;

    public static TimePickerDialog init() {
        TimePickerDialog build = (TimePickerDialog) new Builder()
                .setLayoutId(R.layout.dialog_bottom_time_picker)
                .setInterval(0)
                .setTitle("请选择")
                .build(new TimePickerDialog());
        return build;
    }

    @Override
    protected void initData() {
        super.initData();
        mDialog.findViewById(R.id.ok).setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (!mTimePicker.canSelected()) return;//  滑动未停止不响应点击事件
                mTimePicker.onConfirm();
                dismissDialog(getAttachActivity());
            }
        });
        mTimePicker =
                new TimePicker.Builder(mActivity, TimePicker.TYPE_DATE ,
                        this)
                        // 设置不包含超出的结束时间<=
                        .setContainsEndDate(true)
                        .dialog(this)
                        .setContainsStarDate(true)

                        // 设置时间间隔为30分钟
//                        .setTimeMinuteOffset(30).setRangDate(1517771651000L, 1577976666000L)
                        .setFormatter(new TimePicker.DefaultFormatter() {
                            @Override
                            public CharSequence format(TimePicker picker, int type, int position, long value) {
                                if (type == TimePicker.TYPE_MIXED_DATE) {
                                    CharSequence text;
                                    int dayOffset = DateUtil.getDayOffset(value, System.currentTimeMillis());
                                    if (dayOffset == 0) {
                                        text = "今天";
                                    } else if (dayOffset == 1) {
                                        text = "明天";
                                    } else { // xx月xx日 星期 x
                                        text = mDateFormat.format(value);
                                    }
                                    return text;
                                }
                                return super.format(picker, type, position, value);
                            }
                        })
                        .create();
        mTimePicker.setSelectedDate(System.currentTimeMillis());
    }

    @Override
    public void onCreate(BasePicker picker) {
        //初始化Dialog,将pickerContainer添加到dialog跟布局中
        LinearLayout pickerContainer = picker.view();
        LinearLayout root = (LinearLayout) mParentView;
        root.addView(picker.view(), 1);
    }

    public interface TimeClickListener {
        void onClick(String day, String time);
    }

    public TimePickerDialog setTimeClickListener(TimeClickListener listener) {
        this.listener = listener;
        return this;
    }


    @Override
    public void showDialog() {

    }

    @Override
    public void onTimeSelect(TimePicker picker, Date date) {
        long time = date.getTime();
        String t1 = TimeUtil.formatData(TimeUtil.dateFormatHM, time);
        String t2 = TimeUtil.formatData(TimeUtil.dateFormatYMD, time);
        if (listener != null) listener.onClick(t2, t1);
    }
}
